package com.example.leetcode.leetcode;


import com.example.leetcode.leetcode.list.ListNode;

/**
 *  反转链表
 *  */
public class Solution206 {

    public static ListNode reverseList(ListNode head) {
        if( null == head) return null;
        ListNode next = head.next;
        head.next=null;
        return reverse(head,next);
    }
    public static ListNode reverse (ListNode head , ListNode tail){
        if (tail==null){
            return head;
        }
        ListNode next = tail.next;
        tail.next = head ;
        return reverse(tail,next);
    }
    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        node1.next=node2;
        node2.next=node3;
        ListNode temp = reverseList(node1);
        while(temp!=null){
            System.out.println(temp.val);
            temp = temp.next;
        }
    }
}